package com.jspgou.cms.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.cms.dao.AftersaleTypeDao;
import com.jspgou.cms.entity.AftersaleType;
import com.jspgou.cms.manager.AftersaleDemandMng;
import com.jspgou.cms.manager.AftersaleTypeMng;
import com.jspgou.common.hibernate3.Updater;
import com.jspgou.common.page.Pagination;

@Service
@Transactional
public class AftersaleTypeMngImpl implements AftersaleTypeMng {
	@Transactional(readOnly = true)
	public Pagination getPage(int pageNo, int pageSize) {
		Pagination page = dao.getPage(pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public AftersaleType findById(Long id) {
		AftersaleType entity = dao.findById(id);
		return entity;
	}
	
	public List<AftersaleType> findAll(){
		return dao.findAll();
	}
	
	public List<AftersaleType> findByDeliver(Boolean deliver){
		return dao.findByDeliver(deliver);
	}

	public AftersaleType save(AftersaleType bean,Long[] demandIds) {
		dao.save(bean);
		// 保存售后服务要求
		if (demandIds != null && demandIds.length > 0) {
			for (Long id : demandIds) {
				bean.addToDemands(aftersaleDemandMng.findById(id));
			}
		}	
		return bean;
	}

	public AftersaleType update(AftersaleType bean,Long[] demandIds) {
		Updater<AftersaleType> updater = new Updater<AftersaleType>(bean);
		AftersaleType entity = dao.updateByUpdater(updater);
		// 更新售后服务要求
		entity.getDemands().clear();
		if (demandIds != null && demandIds.length > 0) {
			for (Long id : demandIds) {
				entity.addToDemands(aftersaleDemandMng.findById(id));
			}
		}	
		return entity;
	}

	public AftersaleType deleteById(Long id) {
		AftersaleType bean = dao.deleteById(id);
		return bean;
	}
	
	public AftersaleType[] deleteByIds(Long[] ids) {
		AftersaleType[] beans = new AftersaleType[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private AftersaleTypeDao dao;
	private AftersaleDemandMng aftersaleDemandMng;

	@Autowired
	public void setDao(AftersaleTypeDao dao) {
		this.dao = dao;
	}
	@Autowired
	public void setAftersaleDemandMng(AftersaleDemandMng aftersaleDemandMng) {
		this.aftersaleDemandMng = aftersaleDemandMng;
	}
}